<?php

namespace app\wechat\models;

use app\wechat\core\BaseModel;
use cm;
use common\models\Circuit;
use common\models\CircuitAddress;
use common\models\OpenCity;
use common\models\PointTime;
use common\models\Site;
use common\enum\Enum;
use yii\db\Expression;
use Yii;
use yii\helpers\Url;

class HotModel extends BaseModel
{
    /*
    *获取热门站点
    */
    public function actionSite(){
        $ssql = "SELECT id,name,firstchar,'site' as type FROM site WHERE status=1 AND is_hot=1 ORDER BY sort";
        $hotsite = Cm::$db->all($ssql);
        $csql = "SELECT id,name,firstchar,'city' as type FROM open_city WHERE status=1 AND is_hot=1 ORDER BY code";
        $hotcity = Cm::$db->all($csql);
        $hot = [];
        if(!empty($hotsite)){
            foreach ($hotsite as $key => $value) {
                $hot[] = $value;
            }
        }
        if(!empty($hotcity)){
            foreach ($hotcity as $key => $value) {
                $hot[] = $value;
            }
        }
        Cm::$res->susess(['hot'=>$hot]);
    }

    /*
    *获取热门线路排版
    */
    public function  actionCircuit(){
        $page = Cm::$req->post('page', ['null', 'empty', 'default' => 1]);
        $limit = Cm::$req->post('limit', ['null', 'empty', 'default' => 10]);
        $time = date("Y-m-d H:i:s");

        $open_city_data=OpenCity::find()
            ->andWhere(['status'=>Enum::STATUS_NORMAL])
            ->select(['name'])
            ->indexBy('code')
            ->column();
        $open_city_codes=array_keys($open_city_data);
        $site_data=Site::find()
            ->andWhere(['status'=>Enum::STATUS_NORMAL])
            ->select(['name'])
            ->indexBy('id')
            ->column();
        $site_ids=array_keys($site_data);
        $query=PointTime::find()->alias('pt')
            ->leftJoin(['ca'=>CircuitAddress::tableName()],['ca.id'=>new Expression('pt.ca_id')])
            ->leftJoin(['a'=>Circuit::tableName()],['ca.circuit_id'=>new Expression('a.id')])
            ->leftJoin(['s'=>'shift'],['s.id'=>new Expression('pt.shift_id')])
            ->select([
                    's.id','s.s_ticket','pt.departuretime','s.ticket_price','s.circuit_id',
                    'ca.id as ca_id','pt.id as pt_id','a.e_address','a.city_scode','a.city_ecode',
                    'a.site_sid','a.site_eid','ca.site_id','ca.address as s_address'
                ]
            )->andWhere([
                'a.status'=>Enum::STATUS_NORMAL,'a.is_hot'=>Enum::HOT_HOT,
                's.status'=>Enum::STATUS_NORMAL,
                'ca.type'=>CircuitAddress::TYPE_GETON,'ca.is_del'=>CircuitAddress::IS_DEL_NO,
                'a.city_scode'=>$open_city_codes,'a.city_ecode'=>$open_city_codes,
                'a.site_sid'=>$site_ids,'a.site_eid'=>$site_ids,'ca.site_id'=>$site_ids,
            ])->andWhere(['>=','pt.closingtime',$time])
            ->andWhere(['<=','pt.presaletime',$time])
            ->orderBy(['pt.departuretime'=>SORT_ASC,'id'=>SORT_ASC]);
        $resdata['count']=(clone $query)->count();
        $resdata['data'] = Cm::$db->all((clone $query)
            ->limit($limit)->offset(($page - 1) * $limit)->createCommand()->getRawSql());

        if(!empty($resdata['data'])){
            $config = Cm::$db->all("SELECT `key`,`value` FROM config_setting WHERE `group`='sys' AND `key` IN ('sys_service_charge','sys_premium') ");
            $service_charge = 0;
            $premium = 0;
            if(!empty($config)){
                foreach ($config as $key => $value) {
                    if($value['key'] == 'sys_service_charge'){
                        $service_charge = floatval($value['value']);
                    }else if($value['key'] == 'sys_premium'){
                        $premium = floatval($value['value']);
                    }
                }
            }
            foreach ($resdata['data'] as $key => &$value) {
                $departuretime = strtotime($value['departuretime']);
                $value['day'] = date("m月d日",$departuretime);
                $value['hour'] = date("H:i",$departuretime);
                $value['ticket_price'] += $premium+$service_charge;
                $value['s_ticket'] = $this->checkSticket($value['circuit_id'],$value['id'],$value['s_ticket']);
                $value['citysname']=$open_city_data[$value['city_scode']]??'';
                $value['cityename']=$open_city_data[$value['city_ecode']]??'';
                $value['sitesname']=$site_data[$value['site_sid']]??'';
                $value['siteename']=$site_data[$value['site_eid']]??'';
                /*if($value['site_id']==$value['site_sid'])*/ $value['s_address']=$site_data[$value['site_id']]??'';
            }
        }
        Cm::$res->susess($resdata);
    }

    /*
    *获取轮播图
    */
    public function actionBanner(){
        $list = Cm::$db->all("SELECT `path`,link_url FROM banner WHERE status=1 ORDER BY sort ASC");
        if(!empty($list)){
            foreach ($list as $key => &$value) {
                $value['path'] = BASE_URL.$value['path'];
                $value['link_url'] = empty($value['link_url']) ? '' : $value['link_url'];
            }
        }
        Cm::$res->susess(['banner'=>$list]);
    }
}